package org.edu.modules.growth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.growth.config.QueryConditionTool;
import org.edu.modules.growth.dto.VeJsczSummaryConfigDTO;
import org.edu.modules.growth.entity.VeJsczSummary;
import org.edu.modules.growth.entity.bo.VeBaseSemBO;
import org.edu.modules.growth.mapper.IVeJsczSummaryMapper;
import org.edu.modules.growth.service.IVeBaseManageService;
import org.edu.modules.growth.service.IVeJsczConfigService;
import org.edu.modules.growth.service.IVeJsczSummaryService;
import org.edu.modules.growth.vo.QueryCondition;
import org.edu.modules.growth.vo.SummaryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class VeJsczSummaryServiceImpl extends ServiceImpl<IVeJsczSummaryMapper,
        VeJsczSummary> implements IVeJsczSummaryService {

    private IVeJsczSummaryMapper mIVeJsczSummaryMapper;

    private IVeJsczConfigService mIVeJsczConfigService ;

    private IVeBaseManageService mIVeBaseManageService ;



    @Override
    public SummaryVo init() {
        SummaryVo vo = new SummaryVo() ;
        vo.setCode(1) ;
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 查询当前年份的年终终结提交时间
        VeJsczSummaryConfigDTO ve = mIVeJsczConfigService.getCurrentConfig() ;
        //当前时间
        Long time = System.currentTimeMillis()/1000 ;
        if( time > ve.getBeginTime() && time < ve.getEndTime() ){
            vo.setCode(0) ;
            // 获取当前学年
            List<VeBaseSemBO> list = mIVeBaseManageService.semesterVo().getResult() ;
            list = list.stream().filter(l-> l.getIscurrent()==1).collect(Collectors.toList());
            VeBaseSemBO sem = list.get(0) ;
            Map<String,Object> map = new HashMap<>() ;
            map.put("yearCode",sem.getXn()) ;
            map.put("teacherUserId",user.getId()) ;
            List<VeJsczSummary> summaryListlist = mIVeJsczSummaryMapper.selectByMap(map) ;
            if( !CollectionUtils.isEmpty(summaryListlist)){
                vo.setSummary(summaryListlist.get(0)) ;
            }
        }else{
            vo.setMsg("当前时间不在年终总结提交时间范围内，无法操作!") ;
        }
       return vo ;
    }

    @Override
    public void add(VeJsczSummary summary) throws Exception {
        Map<String,Object> map = new HashMap<>() ;
        map.put("yearCode",summary.getYearCode()) ;
        map.put("teacherUserId",summary.getTeacherUserId()) ;
        List<VeJsczSummary> summaryListlist = mIVeJsczSummaryMapper.selectByMap(map) ;
        if( !CollectionUtils.isEmpty(summaryListlist)){
            throw new Exception("本年度年终总结已提交，无法新增！") ;
        }
        mIVeJsczSummaryMapper.insert(summary) ;
    }

    @Override
    public Map<String, Object> selectPage(QueryCondition queryCondition) {
        HashMap<String,Object> map = new HashMap<>();
        IPage<VeJsczSummary> page = new Page<>(queryCondition.getCurrent(), queryCondition.getPageSize());
        QueryWrapper<VeJsczSummary> wrapper = new QueryWrapper<>() ;
        QueryConditionTool.advancedQueryPage(queryCondition.getConditions(),wrapper);
        wrapper.orderByDesc("yearCode");
        page = mIVeJsczSummaryMapper.selectPage(page,wrapper);
        List<VeJsczSummary> list = page.getRecords();
        Integer count = mIVeJsczSummaryMapper.selectCount(wrapper);
        map.put("list",list);
        map.put("count", count);
        return map;
    }

    @Override
    public boolean isPass() {
        // 查询当前年份的年终终结提交时间
        VeJsczSummaryConfigDTO ve = mIVeJsczConfigService.getCurrentConfig() ;
        //当前时间
        Long time = System.currentTimeMillis()/1000 ;
        if( time > ve.getBeginTime() && time < ve.getEndTime() ){
            return true ;
        }else{
            return false ;
        }
    }


}
